<template>
  <el-header>
    <div class="l-content">
      <el-button size="small" plain @click="handleCollapse">
        <el-icon :size="20">
          <Menu />
        </el-icon>
      </el-button>
      <!-- 面包屑 -->
      <el-breadcrumb separator="/" class="bread">
        <!-- 首页是一定存在的所以写死 -->
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="current.path" v-if="current">{{ current.label }}</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="r-content">
      <el-dropdown>
        <span class="el-dropdown-link">
          <img class="user" :src="getImgSrc('person')" alt="" />
        </span>
        <template #dropdown>
          <el-dropdown-menu >
            <el-dropdown-item>个人中心</el-dropdown-item>
            <el-dropdown-item @click="handleLoginOut">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </el-header>
</template>

<script setup>
import { computed } from 'vue';
import { useRouter } from 'vue-router';
import { useStore } from 'vuex';
let store = useStore();
const router = useRouter();
let getImgSrc = (user) => {
  return new URL(`../assets/images/${user}.png`, import.meta.url).href;
};

let handleCollapse = () => {
  // 调用vuex中的mutations
  store.commit("updateIsCollapse");
};

// 计算属性
const current = computed(() => {
  return store.state.currentMenu;
});

// 退出登录
const handleLoginOut = () => {
  store.commit('clearMenu');
  store.commit('clearToken');
  store.commit('clearTabsList');

  localStorage.removeItem('teacher');
  localStorage.removeItem('admin');
  localStorage.removeItem('student');
  router.push({
    name: "login",
  })
};
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fff;
  padding: 0 24px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  height: 56px;
}

.l-content {
  display: flex;
  align-items: center;

  .el-button {
    margin-right: 16px;
    border: none;

    &:hover {
      background-color: rgba(24,144,255,0.1);
    }
  }

  .bread {
    font-size: 14px;
    .el-breadcrumb__inner {
      color: #606266;
    }
    .el-breadcrumb__item + .el-breadcrumb__separator {
      margin: 0 8px;
      color: #c0c4cc;
    }
  }
}

.r-content {
  .el-dropdown-link {
    display: inline-block;
    cursor: pointer;
  }
  .user {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid #1890ff;
  }
}
</style>
